﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
public partial class Manage_Emp2_Plan2Detail : AdminPageBase
{
    DataSet dsclass;
    DataSet dscustomer;
    DataSet dsRecord;
    int id;
	private string returnURL;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        GetClassRecordId();
		returnURL = Request.QueryString["returnURL"].ToString();
        if (ViewState["tbClass"] == null)
        {
            dsclass = tbClass() ;

            ViewState["tbClass"] = dsclass;
        }
        else
            dsclass = (DataSet)ViewState["tbClass"];
        if (ViewState["tbCustomer"] == null)
        {
            dscustomer = tbCustomer();

            ViewState["tbCustomer"] = dscustomer;
        }
        else
            dscustomer = (DataSet)ViewState["tbCustomer"];
        if (ViewState["tbRecord"] == null)
        {
            dsRecord =tbRecord();

            ViewState["tbRecord"] = dsRecord;
        }
        else
            dsRecord = (DataSet)ViewState["tbRecord"];

        if (!IsPostBack)
        {

            // 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression", "RecordId");
            this.GridView1.Attributes.Add("SortDirection", "ASC");
            this.GridView2.Attributes.Add("SortExpression", "CustomerId");
            this.GridView2.Attributes.Add("SortDirection", "ASC");
            this.GridView3.Attributes.Add("SortExpression", "ClassId");
            this.GridView3.Attributes.Add("SortDirection", "ASC");
            DataTable dt = new DataTable();
            dt.Columns.Add("RecordId");
            dt.Columns.Add("CenterName");
            dt.Columns.Add("CenterId");
            dt.Columns.Add("CustomerCode");
            dt.Columns.Add("CustomerId");
            dt.Columns.Add("CustomerName");
            dt.Columns.Add("ClassTypeName");
            dt.Columns.Add("ClassCode");
            dt.Columns.Add("ClassId");
            dt.Columns.Add("ClassName");
            dt.Columns.Add("Time");
            dt.Columns.Add("PlanTimes");
            dt.Columns.Add("OpenTime");
            dt.Columns.Add("FactTimes");
            dt.Columns.Add("TeacherName");
            dt.Columns.Add("GradeName");
            dt.Columns.Add("GradeId");
            dt.Columns.Add("");
            GridviewControl.GridViewDataBind(this.GridView1, dt);
            GridviewControl.ResetGridView(this.GridView1);
            BindRecord();
        }
    }
    protected void GetClassRecordId()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;

        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);

        }
        if (Request.QueryString["Num"] == null)
        {
            lblNum.Text = "";
        }
        else
            lblNum.Text = Request.QueryString["Num"].ToString();
        if (Request.QueryString["Title"] == null)
            txttitle.Text = "";
        else
            txttitle.Text = Request.QueryString["Title"].ToString();
    }
    protected void BindRecord()
    {

        if (id != 0)
        {
            DataSet dsemp = jtDataAccess.GetGetRecordByClassRecordId(id);
            if (CoreTools.CheckDataSet(dsemp))
            {
                // 获取GridView排序数据列及排序方向
                string sortExpression = this.GridView1.Attributes["SortExpression"];
                string sortDirection = this.GridView1.Attributes["SortDirection"];

                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dsemp.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                //this.GridView1.DataSource = dsemp.Tables[0];
                //this.GridView1.DataBind();
                GridviewControl.GridViewDataBind(this.GridView1, dsemp.Tables[0]);
                ViewState["tbRecord"] = dsemp;
                dsRecord = dsemp;

            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("RecordId");
                dt.Columns.Add("CenterName");
                dt.Columns.Add("CenterId");
                dt.Columns.Add("CustomerCode");
                dt.Columns.Add("CustomerId");
                dt.Columns.Add("CustomerName");
                dt.Columns.Add("ClassTypeName");
                dt.Columns.Add("ClassCode");
                dt.Columns.Add("ClassId");
                dt.Columns.Add("ClassName");
                dt.Columns.Add("Time");
                dt.Columns.Add("PlanTimes");
                dt.Columns.Add("OpenTime");
                dt.Columns.Add("FactTimes");
                dt.Columns.Add("TeacherName");
                dt.Columns.Add("GradeName");
                dt.Columns.Add("GradeId");
                dt.Columns.Add("");
                GridviewControl.GridViewDataBind(this.GridView1, dt);
               
            }
        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void BindClass()
    {
        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView3.Attributes["SortExpression"];
        string sortDirection = this.GridView3.Attributes["SortDirection"];

        string keyword = txtkey.Text.Trim();
        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.SearchClass(keyword, pager2.PageSize, pager2.CurrentPageIndex, sortExpression, sortDirection);
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                this.GridView3.DataSource = ds.Tables[0];
                this.GridView3.DataBind();
                pager2.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);
            }
            else
            {
                this.GridView3.DataSource = null;
                this.GridView3.DataBind();
                pager2.RecordCount = 0;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtkey.Focus();
        }

    }
    protected void ibtnsearch_Click(object sender, ImageClickEventArgs e)
    {
        GridView2.EditIndex = -1;
        BindCustomer();
    }
    protected void ibtnSearchClass_Click(object sender, ImageClickEventArgs e)
    {
        GridView3.EditIndex = -1;
     
        BindClass();
    }
    protected void pager2_PageChanged(object sender, EventArgs e)
    {
        GridView3.EditIndex = -1;
        BindClass();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
    
        }
    }
    protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView2.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView2.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView2.Attributes["SortExpression"] = sortExpression;
        this.GridView2.Attributes["SortDirection"] = sortDirection;

        this.BindCustomer();
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "sel")
        {
            int editflag = Convert.ToInt32(hdfeditflag.Value);
            if (editflag == 0)
            {
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblcentername = (Label)(drv.FindControl("lblcentername"));
                HiddenField hidcenterId = (HiddenField)(drv.FindControl("hidcenterId"));
                Label lblcode = (Label)(drv.FindControl("lblcode"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                dscustomer.Tables[0].Rows.Clear();
                DataRow row = dscustomer.Tables[0].NewRow();
                row["CustomerId"] = e.CommandArgument.ToString(); 
                row["CenterId"] = hidcenterId.Value;
                row["CenterName"] = lblcentername.Text;
                row["CustomerName"] = lblname.Text;
                row["CustomerCode"] = lblcode.Text;
                dscustomer.Tables[0].Rows.Add(row);
                ViewState["tbCustomer"] = dscustomer;
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("客户选择成功，请继续选择课程"), false);
                divSCustomer.Visible = false;
                divSClass.Visible = true;
                floatBoxBg1.Visible = false;
                floatBoxBg2.Visible = true;
            }
            else
            {
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblcentername = (Label)(drv.FindControl("lblcentername"));
                HiddenField hidcenterId = (HiddenField)(drv.FindControl("hidcenterId"));
                Label lblcode = (Label)(drv.FindControl("lblcode"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                Label lblCenterName = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblCenterName");
                HiddenField hdfcenterId = (HiddenField)GridView1.Rows[GridView1.EditIndex].FindControl("hdfcenterId");
                Label lblCustomerCode = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblCustomerCode");
                HiddenField hdfCustomerId = (HiddenField)GridView1.Rows[GridView1.EditIndex].FindControl("hdfCustomerId");
                Label lblCustomerName = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblCustomerName");
               
                lblCustomerName.Text = lblname.Text;
                hdfCustomerId.Value= e.CommandArgument.ToString();
                hdfcenterId.Value = hidcenterId.Value;
                lblCustomerCode.Text = lblcode.Text;
                lblCenterName.Text = lblcentername.Text;
                
                divSCustomer.Visible = false;
                floatBoxBg1.Visible = false;
            }
        }

    }
    protected void pager1_PageChanged(object sender, EventArgs e)
    {
        GridView2.EditIndex = -1;
        BindCustomer();
    }
    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "sel")
        {
            int editflag = Convert.ToInt32(hdfeditflag.Value);
            if (editflag == 0)
            {
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblTypename = (Label)(drv.FindControl("lblTypename"));
                Label lblTime = (Label)(drv.FindControl("lblTime"));
                Label lblCode = (Label)(drv.FindControl("lblCode"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                Label lblIsValid = (Label)(drv.FindControl("lblIsValid"));
                dsclass.Tables[0].Rows.Clear();
                DataRow row = dsclass.Tables[0].NewRow();
                row["ClassId"] = e.CommandArgument.ToString();
                row["ClassTypeName"] = lblTypename.Text;
                row["Time"] = lblTime.Text;
                row["ClassName"] = lblname.Text;
                row["ClassCode"] = lblCode.Text;
                row["IsValidDesc"] = lblIsValid.Text;
                dsclass.Tables[0].Rows.Add(row);
                ViewState["tbClass"] = dsclass;
                divSClass.Visible = false;
                floatBoxBg2.Visible = false;
                int recordId = AddRecord();
                if (recordId == 0)
                {
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加计划课程开设记录失败。"), false);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加计划课程开设记录成功。"), false);
                    //dsRecord.Tables[0].Rows.Clear();
                    //if (ViewState["tbRecord"] != null)
                    //    dsRecord =(DataSet)ViewState["tbRecord"];
                    //DataRow row1 = dsRecord.Tables[0].NewRow();
                    //row1["RecordId"] = recordId;

                    //row1["CenterName"] =dscustomer.Tables[0].Rows[0]["CenterName"].ToString();
                    //row1["CenterId"] = dscustomer.Tables[0].Rows[0]["CenterId"];
                    //row1["CustomerCode"] = dscustomer.Tables[0].Rows[0]["CustomerCode"].ToString();
                    //row1["CustomerId"] = dscustomer.Tables[0].Rows[0]["CustomerId"];
                    //row1["CustomerName"] = dscustomer.Tables[0].Rows[0]["CustomerName"].ToString();
                    //row1["ClassTypeName"] = dsclass.Tables[0].Rows[0]["ClassTypeName"].ToString();
                    //row1["ClassCode"] = dsclass.Tables[0].Rows[0]["ClassCode"].ToString();
                    //row1["ClassId"] = dsclass.Tables[0].Rows[0]["ClassId"];
                    //row1["ClassName"] = dsclass.Tables[0].Rows[0]["ClassName"].ToString();
                    //row1["Time"] = dsclass.Tables[0].Rows[0]["Time"];
                    //row1["PlanTimes"] = 0;
                    //dsRecord.Tables[0].Rows.Add(row1);
                    //ViewState["tbRecord"] = dsRecord;
                    //GridView1.DataSource = dsRecord;
                    //GridView1.DataBind();
                    //GridView1.EditIndex = -1;
                    GridView1.EditIndex = -1;
                    BindRecord();
                    GridView1.EditIndex = GridView1.Rows.Count - 1;
                    GridView1.DataBind();
                    //for (int i = 0; i < GridView1.Rows.Count; i++)
                    //{
                    //    HiddenField hdfRecordId = (HiddenField)GridView1.Rows[i].FindControl("hdfRecordId");
                    //    if (hdfRecordId != null && hdfRecordId.Value == recordId.ToString())
                    //    {
                    //        GridView1.EditIndex = i;
                    //        GridView1.DataBind();
                    //        break;
                    //    }
                    //}
                }
            }
            else
            {
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblTypename = (Label)(drv.FindControl("lblTypename"));
                Label lblTime = (Label)(drv.FindControl("lblTime"));
                Label lblCode = (Label)(drv.FindControl("lblCode"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                Label lblIsValid = (Label)(drv.FindControl("lblIsValid"));

                Label lblClassTypeName = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblClassTypeName");
                Label lblClassCode = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblClassCode");
                HiddenField hdfClassId = (HiddenField)GridView1.Rows[GridView1.EditIndex].FindControl("hdfClassId");
                Label lblClassName = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblClassName");
                TextBox txtTime = (TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("txtTime");

                lblClassTypeName.Text = lblTypename.Text;
                lblClassCode.Text = lblCode.Text;
                lblClassName.Text = lblname.Text;
                txtTime.Text = lblTime.Text;
                hdfClassId.Value = e.CommandArgument.ToString();
                divSClass.Visible = false;
                floatBoxBg2.Visible = false;

            }
           
        }

    }
    protected int AddRecord()
    {
        int flag = 0;
        UserType ut = new UserType();//用户自定义类型
        DataTable RecordTable = ut.GetRecordTableType();
        DataRow RecordTableRow; 
        RecordTableRow = RecordTable.NewRow();
        RecordTableRow["RecordId"] = 0;
        RecordTableRow["ClassRecordId"] = id;
        RecordTableRow["EmpTypeId"] = 2;
        RecordTableRow["ClassId"] = Convert.ToInt32(dsclass.Tables[0].Rows[0]["ClassId"]);

        RecordTableRow["Time"] = Convert.ToDecimal(dsclass.Tables[0].Rows[0]["Time"]);
        RecordTableRow["PlanTimes"] = 0;
        RecordTableRow["CenterId"] = Convert.ToInt32(dscustomer.Tables[0].Rows[0]["CenterId"]);
        RecordTableRow["CustomerId"] = Convert.ToInt32(dscustomer.Tables[0].Rows[0]["CustomerId"]);
        RecordTableRow["GradeId"] = 1;
        RecordTableRow["FactTimes"] = 0;
        RecordTableRow["OpenTime"] = "";
        RecordTableRow["FactTimes"] = 0;
      
        RecordTableRow["TeacherName"] ="";

        RecordTable.Rows.Add(RecordTableRow);
          
        RecordTable.AcceptChanges();
        flag = jtDataAccess.OpRecord(JTDataAccess.OpType.Insert, RecordTable);

        return flag;
    }
    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }
    protected void GridView3_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView3.Attributes["SortExpression"] = sortExpression;
        this.GridView3.Attributes["SortDirection"] = sortDirection;

        this.BindClass();
    }
    protected void BindCustomer()
    {

        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView2.Attributes["SortExpression"];
        string sortDirection = this.GridView2.Attributes["SortDirection"];

        string keyword = txtkeyword.Text.Trim();
        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.SearchCustomer(keyword, pager1.PageSize, pager1.CurrentPageIndex, sortExpression, sortDirection);
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                this.GridView2.DataSource = ds.Tables[0];
                this.GridView2.DataBind();
                pager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);
            }
            else
            {
                this.GridView2.DataSource = null;
                this.GridView2.DataBind();
                pager1.RecordCount = 0;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtkeyword.Focus();
        }


    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindRecord();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            DropDownList ddlGrade = (DropDownList)e.Row.FindControl("ddlGrade");
            HiddenField hdfGradeId = (HiddenField)e.Row.FindControl("hdfGradeId");
            if (ddlGrade != null)
            {
                DataSet ds = jtDataAccess.SearchGrade("",10,0,"Name","ASC");
                if (CoreTools.CheckDataSet(ds))
                {
                    ddlGrade.DataSource = ds;
                    ddlGrade.DataTextField = "Name";
                    ddlGrade.DataValueField = "GradeId";
                    ddlGrade.DataBind();
                    if (hdfGradeId != null)
                        ddlGrade.SelectedValue = hdfGradeId.Value;
                }
            }
        }
       
        if (edit == 1)
            ((CommandField)GridView1.Columns[10]).ShowEditButton = true;
        else
            ((CommandField)GridView1.Columns[10]).ShowEditButton = false;
        if (delete == 1)
            ((CommandField)GridView1.Columns[10]).ShowDeleteButton = true;
        else
            ((CommandField)GridView1.Columns[10]).ShowDeleteButton = false;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        UserType ut = new UserType();//用户自定义类型
        DataTable RecordTable = ut.GetRecordTableType();

        DataRow RecordTableRow;
        int RecordId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());


        RecordTableRow = RecordTable.NewRow();
        RecordTableRow["RecordId"] = RecordId;
        RecordTable.Rows.Add(RecordTableRow);

        RecordTable.AcceptChanges();
        int flag = jtDataAccess.OpRecord(JTDataAccess.OpType.Delete, RecordTable);
        switch (flag)
        {
            case 0:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除失败！"), false);
                break;

            case 1:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除成功！"), false);
                break;
            default:
                break;
        }

        GridView1.EditIndex = -1;
        BindRecord();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        hdfeditflag.Value = "1";
        BindRecord();
        LinkButton lbtnSCustomer = (LinkButton)(GridView1.Rows[e.NewEditIndex].FindControl("lbtnSCustomer"));
        LinkButton lbtnSClass = (LinkButton)(GridView1.Rows[e.NewEditIndex].FindControl("lbtnSClass"));
        if (lbtnSClass != null)
            lbtnSClass.Enabled = true;
        if (lbtnSCustomer != null)
            lbtnSCustomer.Enabled = true;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int flag = 0;
        int RecordId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        int classid = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfClassId")).Value);
        int centerId = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfcenterId")).Value);
        int factTimes = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfFactTimes")).Value);
        int gradeId = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlGrade")).SelectedValue);
        string teachername = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfTeacherName")).Value;
       
        string opentime = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfOpenTime")).Value;
        HiddenField hdfc = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfCustomerId");
        string dd = hdfc.Value;
        int customerId = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfCustomerId")).Value);
        int PlanTimes = 0;
        decimal time = 0;
        TextBox ctime = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTime");
        TextBox cPlanTimes = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPlanTimes");
        if (ctime.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写课程小时数！"), false);
            ctime.Focus();
        }
        else
            time = Convert.ToDecimal(ctime.Text.Trim());
        if (cPlanTimes.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写培训人次！"), false);
            cPlanTimes.Focus();
        }
        else if (!Regex.IsMatch(cPlanTimes.Text.ToString(), "^[1-9]*[1-9][0-9]*$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("培训人次必须为正整数！"), false);
            cPlanTimes.Focus();
        }
        else
            PlanTimes = Convert.ToInt32(cPlanTimes.Text.Trim());


        if (time != 0 && PlanTimes!=0)
        {
            UserType ut = new UserType();//用户自定义类型
            DataTable RecordTable = ut.GetRecordTableType();
            DataRow RecordTableRow;
            RecordTableRow = RecordTable.NewRow();
            RecordTableRow["RecordId"] = RecordId;
            RecordTableRow["ClassRecordId"] = id;
            RecordTableRow["EmpTypeId"] = 2;
            RecordTableRow["ClassId"] = classid;
            RecordTableRow["Time"] = time;
            RecordTableRow["PlanTimes"] = PlanTimes;
            RecordTableRow["CenterId"] = centerId;
            RecordTableRow["CustomerId"] = customerId;
            RecordTableRow["OpenTime"] = opentime;
            RecordTableRow["FactTimes"] = factTimes;
            RecordTableRow["CustomerId"] = customerId;
            RecordTableRow["TeacherName"] = teachername;
            RecordTableRow["GradeId"] = gradeId;
          
            RecordTable.Rows.Add(RecordTableRow);
            flag = jtDataAccess.OpRecord(JTDataAccess.OpType.Update, RecordTable);
            switch (flag)
            {
                case 1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，请重试！"), false);
                    break;
                default:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);

                    break;
            }
            GridView1.EditIndex = -1;
            BindRecord();
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.BindRecord();
    }
    protected void btnAddClass_Click(object sender, EventArgs e)
    {
        hdfeditflag.Value = "0";
        divSCustomer.Visible = true;
       
        floatBoxBg1.Visible = true;
    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
        //Response.Redirect("Plan2.aspx?MenuId="+MenuId);
		Response.Redirect(returnURL);
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        int flag = SaveClassRecord();
        switch (flag)
        {
            case -1:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，已经存在相同编号的课程！"), false);
                break;
            case 0:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                break;
            default:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                break;
        }
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected int SaveClassRecord()
    {
        int flag = 0;
        string Num = lblNum.Text;

        string title = txttitle.Text.Trim();
        if (!CoreTools.ProcessSqlStr(title, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txttitle.Focus();
        }
        else
        {
            flag = jtDataAccess.OpClassRecord(JTDataAccess.OpType.Update, id, 2, Num, title, int.Parse(AdminSessionHelper.UserDetails["UserId"].ToString()), 0);

        }
        return flag;

    }
    protected void ibtncloseCustomerSelect_Click(object sender, ImageClickEventArgs e)
    {
        divSCustomer.Visible = false;
       
        floatBoxBg1.Visible = false;
    }
    protected void ibtncloseClassSelect_Click(object sender, ImageClickEventArgs e)
    {
        divSClass.Visible = false;
        floatBoxBg2.Visible = false;

    }
    protected DataSet tbClass()
    {
        DataSet ds = new DataSet();

        ds.Tables.Add();
        ds.Tables[0].TableName = "tb";
        ds.Tables[0].Columns.Add("ClassId", typeof(int));
        ds.Tables[0].Columns.Add("ClassTypeName", typeof(string));
        ds.Tables[0].Columns.Add("ClassCode", typeof(string));
        ds.Tables[0].Columns.Add("ClassName", typeof(string));
        ds.Tables[0].Columns.Add("Time", typeof(decimal));
        ds.Tables[0].Columns.Add("IsValidDesc", typeof(string));
        return ds;
    }

    protected DataSet tbCustomer()
    {
        DataSet ds = new DataSet();

        ds.Tables.Add();
        ds.Tables[0].TableName = "tb";
        ds.Tables[0].Columns.Add("CustomerId", typeof(int));
        ds.Tables[0].Columns.Add("CenterName", typeof(string));
        ds.Tables[0].Columns.Add("CenterId", typeof(int));
        ds.Tables[0].Columns.Add("CustomerCode", typeof(string));
        ds.Tables[0].Columns.Add("CustomerName", typeof(string));
       
        return ds;
    }

    protected DataSet tbRecord()
    {
        DataSet ds = new DataSet();

        ds.Tables.Add();
        ds.Tables[0].TableName = "tb";
        ds.Tables[0].Columns.Add("RecordId", typeof(int));
        ds.Tables[0].Columns.Add("CustomerId", typeof(int));
        ds.Tables[0].Columns.Add("CenterName", typeof(string));
        ds.Tables[0].Columns.Add("CenterId", typeof(int));
        ds.Tables[0].Columns.Add("CustomerCode", typeof(string));
        ds.Tables[0].Columns.Add("CustomerName", typeof(string));
        ds.Tables[0].Columns.Add("ClassId", typeof(int));
        ds.Tables[0].Columns.Add("ClassTypeName", typeof(string));
        ds.Tables[0].Columns.Add("ClassCode", typeof(string));
        ds.Tables[0].Columns.Add("ClassName", typeof(string));
        ds.Tables[0].Columns.Add("Time", typeof(decimal));
        ds.Tables[0].Columns.Add("PlanTimes", typeof(int));
        return ds;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "selCustomer")
        {
            divSCustomer.Visible = true;
            floatBoxBg1.Visible = true;
            hdfeditflag.Value = "1";
        }
        if (e.CommandName == "selClass")
        {
            divSClass.Visible = true;
            floatBoxBg2.Visible = true;
            hdfeditflag.Value = "1";
        }
    }
    protected void btnInput_Click(object sender, EventArgs e)
    {
        try
        {
            if (fuExcel.HasFile)
            {
                string fileType = fuExcel.FileName.Substring(fuExcel.FileName.LastIndexOf(".") + 1).ToUpper();

                if (fileType == "XLS" || fileType == "XLSX")
                {
                    DataTable dt = jtDataAccess.readExcelDataFromClient(fuExcel, "Sheet1", "Sheet1");
                    UserType ut = new UserType();//用户自定义类型
                    DataTable RecordTable = ut.GetRecordTableType();
                  
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            DataRow RecordTableRow;
                            RecordTableRow = RecordTable.NewRow();
                          
                            RecordTableRow["ClassRecordId"] = id;
                            RecordTableRow["EmpTypeId"] = 2;
                            RecordTableRow["ClassTypeName"] = row["课程类型"];
                            RecordTableRow["ClassCode"] = row["课程编号"];
                            RecordTableRow["ClassName"] = row["课程名称"];
                            RecordTableRow["Time"] = row["课程小时数"];
                            RecordTableRow["PlanTimes"] = row["计划培训人次"];
                            RecordTableRow["CenterName"] = row["所属指挥中心"];
                            RecordTableRow["CustomerName"] = row["客户名称"];
                            RecordTableRow["CustomerCode"] = row["客户编号"];
                            RecordTable.Rows.Add(RecordTableRow);
                        }
                    }

                    RecordTable.AcceptChanges();
                    int flag = jtDataAccess.OpRecord(JTDataAccess.OpType.Update, RecordTable);

                    switch (flag)
                    {
                        case -1:
                            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("Excel文件中有重复的项！"), false);
                            break;
                        case 0:
                            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("没有导入任何记录，请检查Excel文件！"), false);
                            break;
                        default:
                            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(string.Format("成功导入{0}条记录", flag)), false);
                            BindRecord();
                            break;
                    }
                }

            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}